from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
from time import sleep
import os


class Base():
    def __init__(self):
        os.system("adb root")
        cap={
            "appium:platformName": "Android",
            "appium:deviceName": "emulator-5554",
            "appium:appPackage": "com.kongfz.app",
            "appium:appActivity": "com.kongfz.app.home.mvvm.homepage.HomeActivity",
            "appium:noReset": True
        }
        self.devices=webdriver.Remote('http://127.0.0.1:4723/wd/hub',cap)
        self.devices.implicitly_wait(25)
        sleep(15)
    def loc_ele(self,loc):
        try:
            return self.devices.find_element(*loc)
        except Exception as e:
            print("报错信息：",e)
            print("未定位到元素的变量值：",loc)
    def loc_ui_ele(self,loc):
        try:
            return WebDriverWait(self.devices,15).until(Ec.presence_of_element_located(loc))
        except Exception as e:
            print("报错信息：",e)
            print("未定位到元素的变量值：",loc)
    def loc_wait_ele(self,loc):
        try:
            return self.devices.find_element_by_android_uiautomator(loc)
        except Exception as e:
            print("报错信息：",e)
            print("未定位到元素的变量值：",loc)

    def get_key_ele(self,loc,key):
        if type(loc)==str:
            e=self.loc_ui_ele(loc)
        else:
            e=self.loc_ele(loc)
        return e.get_attribute(key)

    def quit_devices(self):
        self.devices.quit()





